const express = require("express")
const router = express.Router()
const Banner = require("../controllers/bannerController")
const banner = new Banner()

router.get('/detail', async (req, res) => {
    let id = req.query.id || 1;
    await banner.detail(id).then(
        result => {
            res.send({
                result,
                msg: "根据id查询轮播图记录",
                code: 200
            })
        },
        err => {
            res.send({
                err,
                msg: "获取轮播图数据失败",
                code: -1
            })
        }
    )
})

router.get('/list', async (req, res) => {
    await banner.list().then(
        result => {
            res.send({
                result,
                msg: "查询轮播图所有记录",
                code: 200
            })
        },
        err => {
            res.send({
                err,
                msg: "获取轮播图数据失败",
                code: -1
            })
        }
    )



})
router.get('/search', async (req, res) => {
    let keyword = req.query.keyword;
    await banner.search(keyword).then(
        result => {
            res.send({
                result,
                msg: "根据关键字查询轮播图记录",
                code: 200
            })
        },
        err => {
            res.send({
                err,
                msg: "获取轮播图数据失败",
                code: -1
            })
        }
    )

})

router.get('/add', async (req, res) => {
    let { banner_title, banner_link, banner_url } = req.query;
    if (banner_title == undefined || banner_link == undefined || banner_url == undefined) {
        res.send({
            code: -1,
            msg: "缺少参数",
        })
    }
    else {
        await banner.add(banner_title, banner_link, banner_url).then(
            result => {
                res.send({
                    result,
                    code: 200,
                    msg: "添加轮播图记录",
                })
            },
            err => {
                res.send({
                    err,
                    msg: "获取轮播图数据失败",
                    code: -1
                })
            }
        )

    }
})

router.get('/modify', async (req, res) => {
    let { banner_title, banner_link, banner_url, id } = req.query;
    if (banner_title == undefined || banner_link == undefined || banner_url == undefined || id == undefined) {
        res.send({
            code: -1,
            msg: "缺少参数",
        })
    }
    else {
        await banner.modify(banner_title, banner_link, banner_url, id).then(
            result => {
                res.send({
                    result,
                    code: 200,
                    msg: "根据id修改轮播图记录",
                })
            },
            err => {
                res.send({
                    err,
                    msg: "获取轮播图数据失败",
                    code: -1
                })
            }
        )


    }
})

router.get('/remove', async (req, res) => {
    let { id } = req.query;
    if (id == undefined) {
        res.send({
            code: -1,
            msg: "缺少参数",
        })
    }
    else {
        await banner.remove(id).then(
            result => {
                res.send({
                    result,
                    code: 200,
                    msg: "根据id删除轮播图记录",
                })
            },
            err => {
                res.send({
                    err,
                    msg: "获取轮播图数据失败",
                    code: -1
                })
            }
        )

    }
})

router.get('/items', async (req, res) => {
    let { page_num, page_size } = req.query;
    if (page_num == undefined || page_size == undefined) {
        res.send({
            code: -1,
            msg: "缺少参数",
        })
    }
    else {
        let page_num2 = parseInt(page_num)
        let page_size2 = parseInt(page_size)
        await banner.items(page_num2, page_size2).then(
            result => {
                res.send({
                    result,
                    code: 200,
                    msg: "获取轮播图分页数据",
                })
            },
            err => {
                res.send({
                    err,
                    msg: "获取轮播图数据失败",
                    code: -1
                })
            }
        )
    }
})

module.exports = router;